package com.tushu.service;

import com.tushu.Login;
import com.tushu.dao.BookDao;
import com.tushu.model.Book;
import com.tushu.model.Student;

import java.util.Scanner;

public class LendService {
    private static BookDao bookDao = new BookDao();

    public static void toLend(Book book,Student student) {
        if (book == null) {
            System.out.println("没有找到相关图书！");
            lendBook(student);
        } else {
            System.out.println("查找结果如下：");
            System.out.println("id" + "\t\t" + "isbn" + "\t\t" + "书名" + "\t\t" + "作者" + "\t\t" + "出版社" + "\t\t" + "馆藏数量");
            System.out.println(book.getId() + "\t\t" + book.getIsbn() + "\t\t" + book.getBookName() + "\t\t" + book.getAuthor() + "\t\t" + book.getPublisher() + "\t\t" + book.getNum());
            System.out.println("-------1.借阅  2.重新输入查询  3.返回------");
            Scanner s3 = new Scanner(System.in);
            System.out.print("请选择：");
            int i = s3.nextInt();
            if (i == 1) {
                //    借阅
                System.out.println("--借书登记--");
                int i1 = bookDao.addRecord(student.getId(), book.getId());
                if (i1 == 1) {
                    System.out.println("借阅成功！");
                    //馆藏量-1
                    bookDao.updateBook1(book.getNum() - 1, book.getId());
                    Login.StudentMenu();
                } else {
                    System.out.println("操作失败！请重试");
                    Login.StudentMenu();
                }
            }
            if (i == 2) {
                lendBook(student);
            }
            if (i == 3) {
                Login.StudentMenu();
            }
        }
    }
    public static void lendBook(Student student) {
        System.out.println("检索方式：1.书名  2.ISBN码");
        System.out.println("请选择：");
        Scanner s1 = new Scanner(System.in);
        int option = s1.nextInt();
        if (option == 1) {
            System.out.print("请输入书名：");
            Scanner s2 = new Scanner(System.in);
            String bookName = s2.nextLine();
            Book book = bookDao.getBookByName(bookName);
            toLend(book, student);
        } else if (option == 2) {
            System.out.print("请输入图书ISBN：");
            Scanner s3 = new Scanner(System.in);
            String isbn = s3.nextLine();
            Book book = bookDao.getBookByIsbn(isbn);
            toLend(book, student);
        } else {
            System.out.println("输入错误！请重新输入！");
            lendBook(student);
        }
    }
}
